package logic

import (
	"context"
	"go-zero2/rpc/greet2/greet2"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"

	"go-zero2/api/internal/svc"
	"go-zero2/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetGreet2Logic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetGreet2Logic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGreet2Logic {
	return &GetGreet2Logic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// GetGreet2 api 请求 greet2 服务
func (l *GetGreet2Logic) GetGreet2() (resp *types.GreetRes, err error) {
	// todo: add your logic here and delete this line

	conn, err := grpc.Dial("greet2:8081", grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Println(err)
		return nil, err
	}
	client := greet2.NewGreet2Client(conn)
	var greet2_resp *greet2.Response
	greet2_resp, err = client.Ping(context.Background(), &greet2.Request{})
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return &types.GreetRes{Msg: "api_v2 -->>  " + greet2_resp.Pong}, nil
}
